package com.wzh.service;

import com.wzh.dao.CardDao;
import com.wzh.pojo.Po.Card;
import com.wzh.pojo.Po.CardAndUser;
import com.wzh.pojo.Po.User;
import com.wzh.pojo.Req.DataReq;
import com.wzh.pojo.Resp.DataResp;
import com.wzh.util.CommmonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CardService {

	@Autowired
	private CardDao cardDao;

	public DataResp<Object> addCards(Integer card) {
		DataResp<Object> resp=new DataResp<>();
		List<String> data =new ArrayList<>();
		for (int i = 0; i < 100; i++) {
			data.add(CommmonUtil.getUUID());
		}
		Integer code =cardDao.addcards(data);

		if (code>0){
			resp.setMes("操作成功！");
		}else{
			resp.setMes("没发生任何变化！");
		}
		return  resp;
	}

	public DataResp<Object> show(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		List<Card> list= cardDao.findall(req);
		if (list.size()!=0){
			resp.setData(list);
			resp.setMes("查询成功！");
		}else{
			resp.setMes("查询失败！");
		}
		return  resp;
	}
	@Transactional
	public DataResp<Object> delCard(CardAndUser cardAndUser) {
		DataResp<Object> resp=new DataResp<>();
		Integer delcode =cardDao.del(cardAndUser.getCard());
		if (delcode>0){
			Integer vipcode =cardDao.addVip(cardAndUser.getUserid(),cardAndUser.getTime());
			if (vipcode>0){
			resp.setMes("操作成功！");
			}
		}else{
			resp.setMes("没发生任何变化！");
			resp.setCode(204);
		}
		return  resp;
	}
}
